import pymysql
import json
from myapp.utils.connection import POOL

# conn = pymysql.Connect(host='localhost', user='root', passwd='123456'
#                        , db='zhifou', port=3306, charset='utf8')
kw = '企业复工'  # 测试用


def participant_num(key):
    conn = POOL.connection()
    cur = conn.cursor()
    print(key)
    sql1 = "SELECT SUM(reposts_count)+COUNT(mid) FROM {}_findson WHERE pid = 0".format(key)
    cur.execute(sql1)
    weibo_num = int(cur.fetchall()[0][0])
    sql2 = "SELECT SUM(attitudes_count) FROM {}_findson".format(key)
    cur.execute(sql2)
    attitude_num = int(cur.fetchall()[0][0])
    print(weibo_num, attitude_num)

    json_file = []

    set = {}
    set['weibonum'] = weibo_num
    set['paticipants'] = weibo_num + attitude_num

    json_file.append(set)

    cur.close()

    conn.close()
    return {"data":json_file}


def writefile(key):
    jsonData = participant_num(key)
    jsondatar = json.dumps(jsonData, ensure_ascii=False)
    print(jsondatar)
    f = open(r'Participant.json', 'w+', encoding="utf8")
    # 写数据
    f.write(jsondatar)
    # 关闭文件
    f.close()


if __name__ == '__main__':
    writefile(kw)
